package com.android.jingweiapp.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import java.util.Set;

/**
 * Created by cuiwenrui-pc on 15/9/16.
 */
public class PreferenceUtil {

    private static final String SP_NAME = "SP";

    private static SharedPreferences mSharedPreferences;
    private static PreferenceUtil preferenceUtil;
    private static SharedPreferences.Editor mEditor;

    private PreferenceUtil(Context context) {
        mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    public static PreferenceUtil getInstance(Context context) {
        if (preferenceUtil == null) {
            preferenceUtil = new PreferenceUtil(context);
        }
        mEditor = mSharedPreferences.edit();
        return preferenceUtil;
    }

    public void put(String key, Integer value) {
        mEditor.putInt(key, value);
        mEditor.apply();
        mEditor.commit();
    }

    public void put(String key, Float value) {
        mEditor.putFloat(key, value);
        mEditor.commit();
    }

    public void put(String key, Long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    public void put(String key, Boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public void put(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void put(String key, Set<String> value) {
        mEditor.putStringSet(key, value);
        mEditor.commit();
    }

    public Integer get(String key, Integer defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    public Boolean get(String key, Boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public Float get(String key, Float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    public Long get(String key, Long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    public String get(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public Set<String> get(String key, Set<String> defaultValue) {
        return mSharedPreferences.getStringSet(key, defaultValue);
    }

    public void remove(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }


}
